package com.lcz.authority.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.lcz.authority.admin.service.SysPostService;
import com.lcz.authority.common.constant.HttpStatus;
import com.lcz.authority.common.core.page.TableDataInfo;
import com.lcz.authority.common.domain.AjaxResult;
import com.lcz.authority.common.domain.entity.SysPostEntity;
import com.lcz.authority.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lcz
 * @desc 岗位信息操作处理
 * @mail lcz_0130@163.com
 * @date 2024/06/17
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController {
    @Autowired
    private SysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPostEntity post) {
        List<SysPostEntity> list = postService.selectPostList(post);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostEntity post) {
        List<SysPostEntity> list = postService.selectPostList(post);
        ExcelUtil<SysPostEntity> util = new ExcelUtil<SysPostEntity>(SysPostEntity.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPostEntity post) {
        if (!postService.checkPostNameUnique(post)) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setCreateBy(getUsername());
        return postService.insertPost(post) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPostEntity post) {
        if (!postService.checkPostNameUnique(post)) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setUpdateBy(getUsername());
        return postService.updatePost(post) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return postService.deletePostByIds(postIds) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<SysPostEntity> posts = postService.selectPostAll();
        return AjaxResult.success(posts);
    }
}
